from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, HumanMessagePromptTemplate, \
    MessagesPlaceholder
from models import get_ds_model_client
from langchain_community.chat_message_histories import ChatMessageHistory

client = get_ds_model_client()

messages = ChatPromptTemplate.from_messages(
    [
        SystemMessagePromptTemplate.from_template("你是人工智能助手"),
        MessagesPlaceholder(variable_name="messages")
    ])

parser = StrOutputParser()

chain = messages | client | parser

# 消息历史组件
chat_history = ChatMessageHistory()
chat_history.add_user_message("你好，我是kenney")
response = chain.invoke({"messages": chat_history.messages})
chat_history.add_ai_message(response)
chat_history.add_user_message("你好, 我是谁?")
print(chain.invoke({"messages": chat_history.messages}))
